# Run preamble
source("code/preamble.R")

# Load data
study2_data <- readRDS("data/study2_data.rds")
study3_data <- readRDS("data/study3_data.rds")
study5_data <- readRDS("data/study5_data.rds")

# Subset Study 2 data
study2_data <- study2_data %>%
    subset(condition == "point_no_incen")

# Subset Study 3 data
study3_data <- study3_data %>%
    subset(
        grepl("^mean_", condition)
    )

# Subset Study 5 data
study5_data <- study5_data %>%
    subset(condition == "mean_no_correct")

# Fill out_point with post_dist_out_mean
study5_data$out_point[is.na(study5_data$out_point)] <-
    study5_data$post_dist_out_mean[is.na(study5_data$out_point)]

# Bind data from different studies
bound_data <- bind_rows(
    study2_data, study3_data, study5_data
) %>%
    select(
        out_point, study,
        pid2, gender, race, latinx, edu_factor, age_factor, study, policy,
        par_pos
    )

# Recode policy variable
bound_data$policy <- bound_data$policy %>%
    factor(
        levels = c("Gun Control", "Border Control", "Abortion Access")
    )

# Generate new variables
bound_data <- bound_data %>%
    mutate(
        par_pos_stereo = case_when(
            pid2 == "Democrat" ~ 10 - par_pos,
            pid2 == "Republican" ~ par_pos
        ),
        out_point_stereo = case_when(
            pid2 == "Republican" ~ 10 - out_point,
            pid2 == "Democrat" ~ out_point
        ),
        most_wording = as.numeric(study == "Study 2")
    ) %>%
    ungroup()

# Model
model <- lm_robust(
    out_point_stereo ~ most_wording + policy + pid2 + gender + race + latinx +
        edu_factor + age_factor + par_pos_stereo,
    data = bound_data
)

# Table
# Regressions
modelsummary(
    list(
        "Perceived Extremity of Average Out-Partisan's Policy Attitude (in Direction of Party Stereotype)" = model
    ),
    stars = TRUE,
    coef_rename = c(
        "(Intercept)",
        'Use of "Most" Wording',
        "Policy Issue is Border Control",
        "Policy Issue is Abortion Access",
        "Participant is Republican",
        "Participant is a Woman",
        "Participant is Another Gender",
        "Participant is Black",
        "Participant is White",
        "Participant is Multi-Racial",
        "Participant is Another Race",
        "Participant is Hispanic",
        "Participant has an Associate's Degree",
        "Participant has a Bachelor's Degree",
        "Participant has a Post-graduate Degree",
        "Participant is 26--34 years old",
        "Participant is 35--49 years old",
        "Participant is 50--64 years old",
        "Participant is 65+ years old",
        "Extremity of Participant's Policy Attitude (in Direction of In-Party Stereotype)"
    ),
    notes = "Note: The reference categories for factor variables are as
    follows: gun control (policy issue), Democrat (partisanship),
    man (gender), Asian (race), not Hispanic (whether Hispanic), high school
    degree or less (education), and 18--25 years old (age). This table includes
    data from Studies 2, 3, and 5.",
    title = "Effect of Question Wording on Perceived Extremity of Average Out-Partisan's Policy Attitude",
    output = "tables/robust_q_wording.txt"
)
